/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.willy.ratingbar;

import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * BaseRatingBar
 *
 * @since 2021-03-26
 */
public class BaseRatingBar extends DirectionalLayout implements Component.TouchEventListener {
    private DirectionalLayout dialayout;
    private Image img1;
    private Image img2;
    private Image img3;
    private Image img4;
    private Image img5;
    private Image img6;
    private Image img7;
    private DirectionalLayout layt1;
    private DirectionalLayout layt2;
    private DirectionalLayout layt3;
    private DirectionalLayout layt4;
    private DirectionalLayout layt5;
    private DirectionalLayout layt6;
    private DirectionalLayout layt7;
    private Component root = null;
    private boolean isTatu = false;
    private List<Image> imageList = null;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public BaseRatingBar(Context context) {
        super(context);
        initview(context);
    }

    /**
     * 构造方法2
     *
     * @param context 上下文
     * @param attrSet 样式
     */
    public BaseRatingBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initview(context);
    }

    /**
     * 构造方法3
     *
     * @param context 上下文
     * @param attrSet 样式
     * @param styleName 名称
     */
    public BaseRatingBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initview(context);
    }

    private void initview(Context context) {
        root = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_imgtwo, this, true);
        dialayout = (DirectionalLayout) root.findComponentById(ResourceTable.Id_Dialayout);
        dialayout.setTouchEventListener(this::onTouchEvent);
        layt1 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt1);
        layt2 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt2);
        layt3 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt3);
        layt4 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt4);
        layt5 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt5);
        layt6 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt6);
        layt7 = (DirectionalLayout) findComponentById(ResourceTable.Id_layt7);

        img1 = (Image) root.findComponentById(ResourceTable.Id_img1);
        img2 = (Image) root.findComponentById(ResourceTable.Id_img2);
        img3 = (Image) root.findComponentById(ResourceTable.Id_img3);
        img4 = (Image) root.findComponentById(ResourceTable.Id_img4);
        img5 = (Image) root.findComponentById(ResourceTable.Id_img5);
        img6 = (Image) root.findComponentById(ResourceTable.Id_img6);
        img7 = (Image) root.findComponentById(ResourceTable.Id_img7);
        img1.setTag(1);
        img2.setTag(NumberUntil.getAnInt2());
        img3.setTag(NumberUntil.getAnInt3());
        img4.setTag(NumberUntil.getAnInt4());
        img5.setTag(NumberUntil.getAnInt5());
        img6.setTag(NumberUntil.getAnInt6());
        img7.setTag(NumberUntil.getAnInt7());

        imageList = new ArrayList<>();
        imageList.add(img1);
        imageList.add(img2);
        imageList.add(img3);
        imageList.add(img4);
        imageList.add(img5);
        imageList.add(img6);
        imageList.add(img7);

        img1.setClickedListener(this::onClick);
        img2.setClickedListener(this::onClick);
        img3.setClickedListener(this::onClick);
        img4.setClickedListener(this::onClick);
        img5.setClickedListener(this::onClick);
        img6.setClickedListener(this::onClick);
        img7.setClickedListener(this::onClick);
        isTatu = true;
        img1.setPixelMap(ResourceTable.Media_filled);
        img2.setPixelMap(ResourceTable.Media_filled);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        float axisX = touchEvent.getPointerPosition(0).getX(); // 屏幕的高度
        switch (touchEvent.getAction()) {
            case TouchEvent.POINT_MOVE: // 表示一直在移动
                setrigat(axisX);
                break;
            case TouchEvent.PRIMARY_POINT_DOWN: // 第一根手指落下
                break;
            case TouchEvent.CANCEL: // 事件中段
            case TouchEvent.PRIMARY_POINT_UP: // 最后一根手指抬起
                break;
            default:
                break;
        }
        return true;
    }

    private void setrigat(float axisX) {
        if (layt1.getContentPositionX() > axisX) {
            if (isTatu) {
                isTatu = false;
                img1.setPixelMap(ResourceTable.Media_empty);
            } else {
                isTatu = true;
                img1.setPixelMap(ResourceTable.Media_filled);
            }
        } else if (layt2.getContentPositionX() > axisX) {
            if (isTatu) {
                isTatu = false;
                img2.setPixelMap(ResourceTable.Media_empty);
                img3.setPixelMap(ResourceTable.Media_empty);
                img4.setPixelMap(ResourceTable.Media_empty);
                img5.setPixelMap(ResourceTable.Media_empty);
                img6.setPixelMap(ResourceTable.Media_empty);
                img7.setPixelMap(ResourceTable.Media_empty);
            } else {
                isTatu = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                img2.setPixelMap(ResourceTable.Media_filled);
            }
        } else if (layt3.getContentPositionX() > axisX) {
            if (isTatu) {
                isTatu = false;
                img3.setPixelMap(ResourceTable.Media_empty);
                img4.setPixelMap(ResourceTable.Media_empty);
                img5.setPixelMap(ResourceTable.Media_empty);
                img6.setPixelMap(ResourceTable.Media_empty);
                img7.setPixelMap(ResourceTable.Media_empty);
            } else {
                isTatu = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                img2.setPixelMap(ResourceTable.Media_filled);
                img3.setPixelMap(ResourceTable.Media_filled);
            }
        } else if (layt4.getContentPositionX() > axisX) {
            allFour();
        } else if (layt5.getContentPositionX() > axisX) {
            allFivelayt();
        } else if (layt6.getContentPositionX() > axisX) {
            allSixlayt();
        } else if (layt7.getContentPositionX() > axisX) {
            allJiuy();
        }
    }

    private void allFour() {
        if (isTatu) {
            isTatu = false;
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else {
            isTatu = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
        }
    }

    private void allFivelayt() {
        if (isTatu) {
            isTatu = false;
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else {
            isTatu = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
        }
    }

    private void allSixlayt() {
        if (isTatu) {
            isTatu = false;
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else {
            isTatu = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
            img6.setPixelMap(ResourceTable.Media_filled);
        }
    }

    private void allJiuy() {
        if (isTatu) {
            isTatu = false;
            img7.setPixelMap(ResourceTable.Media_empty);
        } else {
            isTatu = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
            img6.setPixelMap(ResourceTable.Media_filled);
            img7.setPixelMap(ResourceTable.Media_filled);
        }
    }

    private void onClick(Component component) {
        int id = component.getId();
        if (id == ResourceTable.Id_img1) {
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_empty);
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else if (id == ResourceTable.Id_img2) {
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else if (id == ResourceTable.Id_img3) {
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else if (id == ResourceTable.Id_img4) {
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_empty);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else if (id == ResourceTable.Id_img5) {
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
            img6.setPixelMap(ResourceTable.Media_empty);
            img7.setPixelMap(ResourceTable.Media_empty);
        } else if (id == ResourceTable.Id_img6) {
            allSix();
        } else {
            allFild();
        }
    }

    private void allSix() {
        img1.setPixelMap(ResourceTable.Media_filled);
        img2.setPixelMap(ResourceTable.Media_filled);
        img3.setPixelMap(ResourceTable.Media_filled);
        img4.setPixelMap(ResourceTable.Media_filled);
        img5.setPixelMap(ResourceTable.Media_filled);
        img6.setPixelMap(ResourceTable.Media_filled);
        img7.setPixelMap(ResourceTable.Media_empty);
    }

    private void allFild() {
        img1.setPixelMap(ResourceTable.Media_filled);
        img2.setPixelMap(ResourceTable.Media_filled);
        img3.setPixelMap(ResourceTable.Media_filled);
        img4.setPixelMap(ResourceTable.Media_filled);
        img5.setPixelMap(ResourceTable.Media_filled);
        img6.setPixelMap(ResourceTable.Media_filled);
        img7.setPixelMap(ResourceTable.Media_filled);
    }
}
